<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.key" placeholder="参数名" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button v-if="isAuth('activity:activity:save')" type="primary" @click="addHandle()">新增</el-button>
        <el-button v-if="isAuth('activity:activity:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="dataList" border v-loading="dataListLoading" @selection-change="selectionChangeHandle" style="width: 100%">
      <el-table-column type="selection" header-align="center" align="center" width="50" />
      <el-table-column prop="id" header-align="center" align="center" label="ID" width="65" />
      <el-table-column prop="title" header-align="center" align="center" label="活动标题" />
      <el-table-column prop="originalPrice" header-align="center" align="center" label="原价" width="80">
        <template slot-scope="scope">&yen;{{ scope.row.originalPrice || 0 }}</template>
      </el-table-column>
      <el-table-column prop="buyingPrice" header-align="center" align="center" label="抢购价" width="80">
        <template slot-scope="scope">&yen;{{ scope.row.buyingPrice || 0 }}</template>
      </el-table-column>
      <el-table-column prop="startDate" header-align="center" align="center" label="活动开始日期" />
      <el-table-column prop="endDate" header-align="center" align="center" label="活动结束日期" />
      <el-table-column prop="status" header-align="center" align="center" label="状态" width="80">
        <template slot-scope="scope">{{ scope.row.status === 0 ? "正常" : "结束" }}</template>
      </el-table-column>
      <el-table-column prop="storeName" header-align="center" align="center" label="门店名称" />
      <el-table-column prop="itemsName" header-align="center" align="center" label="项目名称" />
      <el-table-column prop="createTime" header-align="center" align="center" label="创建时间" />
      <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
          <el-button type="text" size="small" @click="seeHandle(scope.row.id)">查看</el-button>
          <el-button type="text" size="small" @click="cardHandle(scope.row.id)">卡券二维码</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex" :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage" layout="total, sizes, prev, pager, next, jumper" />
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>

    <el-dialog :title="!codeTitle ? '手机扫二维码参加活动' : '手机扫二维码查看卡券'" width="40%" :visible.sync="qrCodeDialogVisible" :before-close="qrCodeHandleClose">
      <div style="margin-left: 18%">
        <vue-qr :text="qrcodeImgUrl" :size="400" :correctLevel="0" :margin="3"></vue-qr>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="text" size="small" @click="qrCodeHandleClose">返回</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import VueQr from "vue-qr";
  import AddOrUpdate from "./activity-add-or-update";
  export default {
    data() {
      return {
        dataForm: {
          key: "",
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        qrCodeDialogVisible: false,
        examId: "",
        qrcodeImgUrl: "",
        codeTitle: false,
      };
    },
    components: {
      AddOrUpdate,
      VueQr,
    },
    activated() {
      this.getDataList();
    },
    created() {},
    methods: {
      // 获取数据列表
      getDataList() {
        this.dataListLoading = true;
        this.$http({
          url: this.$http.adornUrl("/activity/activity/list"),
          method: "get",
          params: this.$http.adornParams({
            page: this.pageIndex,
            limit: this.pageSize,
            key: this.dataForm.key,
          }),
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list;
            this.totalPage = data.page.totalCount;
          } else {
            this.dataList = [];
            this.totalPage = 0;
          }
          this.dataListLoading = false;
        });
      },
      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val;
        this.pageIndex = 1;
        this.getDataList();
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageIndex = val;
        this.getDataList();
      },
      // 多选
      selectionChangeHandle(val) {
        this.dataListSelections = val;
      },
      // 新增 / 修改
      addHandle() {
        this.$router.push({ path: "/activity/activity/create" });
      },
      addOrUpdateHandle(id) {
        this.addOrUpdateVisible = true;
        this.$nextTick(() => {
          // this.$refs.addOrUpdate.init(id)
          this.$router.push({
            path: "/activity/activity/create",
            query: { id: id },
          });
        });
      },
      // 删除
      deleteHandle(id) {
        var ids = id ? [id] :
          this.dataListSelections.map((item) => {
            return item.id;
          });
        this.$confirm(
          `确定对[id=${ids.join(",")}]进行[${
                    id ? "删除" : "批量删除"
                }]操作?`,
          "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          }
        ).then(() => {
          this.$http({
            url: this.$http.adornUrl("/activity/activity/delete"),
            method: "post",
            data: this.$http.adornData(ids, false),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.getDataList();
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        });
      },
      async seeHandle(id) {
        this.examId = id;
        this.qrCodeDialogVisible = true;
        this.codeTitle = false;
        await this.qrCodeCreate(false);
      },
      async cardHandle(id) {
        this.examId = id;
        this.qrCodeDialogVisible = true;
        this.codeTitle = true;
        await this.qrCodeCreate(true);
      },
      //关闭弹框清除二维码
      qrCodeHandleClose() {
        this.qrCode2 = "";
        this.qrCodeDialogVisible = false;
      },
      //创建二维码
      qrCodeCreate(bool) {
        let authUtl = bool ? "wechatCoupon" : "wechatOauth";
        let url = `https://shanxihongdi.com/hd-api/app/${authUtl}/${this.examId}`;
        console.log(url);
        this.qrcodeImgUrl = url;
      },
      //二维码显示
      qrCodeOpen(val) {
        this.qrCodeDialogVisible = true;
      },
    },
  };
</script>
